<?php
declare (strict_types=1);

namespace app\business\controller;

use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use app\common\tool\Result;
use think\App;
use think\facade\View;
use think\response\Json;

class OrderRefund extends Base
{

    //主键
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\OrderRefund';

    protected $hasOne = ['user', 'father'];

    protected $append = ['productStatusText', 'statusText', 'product'];

    public function index()
    {
        return $this->setTable();
    }

    //自动构建table,按需修改
    public function setTable()
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['type' => 'checkbox', 'fixed' => 'left', 'field' => '', 'title' => '全选'])
            ->addRow(['field' => 'id', 'title' => 'ID', 'width' => 80])
            ->addRow(['field' => 'account', 'title' => '申请会员', 'templet' => '<div>{{d.user.account}}</div>'])
            ->addRow(['field' => 'type', 'title' => '退款类型', 'templet' => '<div>{{d.type == 1?"退款":"退款退货"}}</div>'])
            ->addRow(['field' => 'sn', 'title' => '退款编号', 'width' => 200])
            ->addRow(['field' => 'order_sn', 'title' => '关联订单编号', 'templet' => '<div>{{d.father.sn}}</div>', 'width' => 200])
            ->addRow(['field' => 'productStatusText', 'title' => '货物状态'])
            ->addRow(['field' => 'reason', 'title' => '退款原因'])
            ->addRow(['field' => 'total', 'title' => '退款总金额'])
            ->addRow(['field' => 'describe', 'title' => '描述信息'])
            ->addRow(['field' => 'images', 'title' => '描述图片', 'templet' => '#imagesTpl'])
            ->addRow(['field' => 'create_time', 'title' => '申请时间', 'width' => 150])
            ->addRow(['field' => 'statusText', 'title' => '状态'])
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 80]);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->addRightButtons(['edit' => ['width' => 1000, 'height' => 1000, 'title' => '详情']])
            ->setSearch($this->setSearch())
            ->addExtraHtml(['ProductEvaluate'])
            ->addExtraMap(['business_id' => $this->business->id])
            ->fetch();
    }

    //自动构建Search，按需修改
    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['uid', '申请会员', 'input', '', '\app\common\model\User', 'account,id'])
            ->setSearch(['sn', '退款编号', 'input'])
            ->setSearch(['order_id', '关联订单编号', 'input', '', '\app\common\model\Product', 'sn,id'])
            ->setSearch(['status', '退款类型', 'select', [1 => '退款', 2 => '退款退货']])
            ->setSearch(['status', '退款状态', 'select', [1 => '等待商家同意', 2 => '等待寄回商品', 3 => '等待商家确认', 4 => '退款成功', 5 => '未通过申请']]);
    }

    public function edit($name = '', $id = '', $tpl = 'order_refund/edit', $info = '')
    {
        return parent::edit($name, $id, $tpl, $info); // TODO: Change the autogenerated stub
    }

    public function examine()
    {
        try {
            $param = $this->request->post();
            $param['type'] = 'examine';
            return Result::fastReturn($this->model::businessEdit($param, $this->business));
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }

    public function receipt()
    {
        try {
            $param = $this->request->post();
            $param['type'] = 'receipt';
            return Result::fastReturn($this->model::businessEdit($param, $this->business));
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
}
